<template>
  <!-- 目录管理 -->
  <div class="app-container">
    <div class="mainbox">
      <!-- 顶部操作 -->
      <div class="filter-container">
        <el-form ref="filterFormRef" :inline="true" :model="filterForm"
                 class="">
          <el-form-item label="菜单名称" prop="menuName">
            <el-input
              v-model.trim="filterForm.menuName"
              class="filter-item limit"
              maxlength="30"
              style="width: 200px;margin-right:10px"
              clearable
              placeholder="请输入内容"
              @keyup.enter.native="handleFilter"
            />
          </el-form-item>
          <el-form-item label="菜单编号" prop="menuCode">
            <el-input
              v-model.trim="filterForm.menuCode"
              class="filter-item limit"
              maxlength="30"
              style="width: 200px;margin-right:10px"
              clearable
              placeholder="请输入内容"
              @keyup.enter.native="handleFilter"
            />
          </el-form-item>
        </el-form>
        <div class="flex-1" />
        <el-button type="primary" @click="handleFilter">搜索</el-button>
        <el-button @click="handleReset">重置</el-button>
      </div>
      <!-- 表格 -->
      <div class="table-container table-fullscreen">
        <div class="table-opt-container">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="onAdd"
          >
            新增
          </el-button>
          <el-button
            icon="el-icon-folder-opened"
            @click="onExpandAll"
          >
            展开所有
          </el-button>
          <el-button
            icon="el-icon-folder"
            @click="onFoldAll"
          >
            折叠所有
          </el-button>
          <div class="flex-1" />
          <dt-dialog-column :columns="columns" @queryTable="getList" />
        </div>
        <el-table
          v-if="refreshTable"
          ref="table"
          v-loading="loading"
          border
          :data="tableData"
          style="width: 100%;margin-bottom: 10px;"
          row-key="id"
          :default-expand-all="isExpandAll"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          :header-cell-style="headerCellStyle"
          :expand-row-keys="expandRowKeys"
        >
          <template slot="empty">
            <p>{{ $store.getters.dataText }}</p>
          </template>
          <el-table-column
            fixed="left"
            type="index"
            label="序号"
            width="70"
            :index="indexMethod"
            align="center"
          />
          <el-table-column
            v-if="columns[0].show"
            key="menuName"
            prop="menuName"
            label="菜单名称"
            :show-overflow-tooltip="true"
            align="center"
          />
          <el-table-column
            v-if="columns[1].show"
            key="menuType"
            prop="menuType"
            label="菜单类型"
            :show-overflow-tooltip="true"
            align="center"
          >
            <template slot-scope="scope">
              <el-tag style="border:none" :type="scope.row.menuType | statusFilter">
                {{ scope.row.menuType == '1' ? '目录': scope.row.menuType == '2' ? '菜单':'未知' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columns[2].show"
            key="menuCode"
            prop="menuCode"
            label="菜单编号"
            :show-overflow-tooltip="true"
            align="center"
          />
          <!-- <el-table-column
            v-if="columns[3].show"
            key="antdvIcon"
            prop="antdvIcon"
            label="图标"
            :show-overflow-tooltip="true"
            align="center"
          >
            <template slot-scope="scope">
              <Item v-if="scope.row.antdvIcon" :icon="scope.row.antdvIcon" />
            </template>
          </el-table-column> -->
          <el-table-column
            v-if="columns[3].show"
            key="sort"
            prop="sort"
            label="排序"
            :show-overflow-tooltip="true"
            align="center"
          />
          <el-table-column
            v-if="columns[4].show"
            key="createTime"
            prop="createTime"
            label="创建时间"
            :show-overflow-tooltip="true"
            align="center"
          />
          <el-table-column
            :key="Math.random()"
            :fixed="$store.getters.device == 'mobile'? null :'right' "
            prop="action"
            label="操作"
            align="center"
          >
            <template slot-scope="scope">
              <el-button
                style="padding:7px 10px"
                size="mini"
                :disabled="scope.row.isDefault && scope.row.isDefault === 2"
                @click="onEdit(scope.row)"
              >
                修改
              </el-button>
              <!-- v-permission="['AUTH_MENU_DEL_BUTTON']" -->
              <el-button
                slot="reference"
                :disabled="scope.row.children && scope.row.children.length > 0||scope.row.isDefault && scope.row.isDefault === 2 "
                size="mini"
                type="danger"
                @click="onDelete(scope.row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :page-size="filterForm.pageSize"
          :current-page.sync="filterForm.pageNo"
          layout="prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <MenuModal ref="menuModal" @added="onAdded" @edited="onEdited" />
  </div>
</template>
<script>
// import Item from '@/framework/layout/components/Sidebar/Item'
import dialogColumn from '@/framework/utils/mixins/dialogColumn'
import MenuModal from './components/MenuModal'
import { getList, del, getVideoNum } from '@/api/classroom/directoryManage'

export default {
  name: 'DirectoryManage',
  components: { MenuModal },
  filters: {
    statusFilter(status) {
      const filter = { M: 'danger', C: 'success' }
      return filter[status]
    }
  },
  mixins: [dialogColumn],
  data() {
    return {
      loading: false,
      refreshTable: true,
      isExpandAll: false,
      filterForm: {
        menuCode: '',
        menuName: ''
      },
      total: 0,
      // 列信息
      columns: [
        { prop: 'menuName', label: '菜单名称', show: true, sortable: true },
        { prop: 'menuType', label: '菜单类型', show: true, sortable: true },
        { prop: 'menuCode', label: '菜单编号', show: true },
        // { prop: 'antdvIcon', label: '图标', show: true },
        { prop: 'sort', label: '排序', show: true },
        { prop: 'createTime', label: '创建时间', show: true }
      ],
      tableData: [],
      headerCellStyle: {
        backgroundColor: '#f2f2f2'
      },
      expandRowKeys: []
    }
  },
  computed: {},
  mounted() {
    this.getList()
  },
  methods: {
    handleSizeChange(val) {
      this.filterForm.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.filterForm.pageNo = val
      this.getList()
    },
    // 列表序号
    indexMethod(index) {
      return index + 1
    },
    // 获取列表
    getList() {
      this.loading = true
      getList(this.filterForm).then((resp) => {
        if (resp.success) {
          this.tableData = [...resp.data]
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 搜索
    handleFilter() {
      this.getList()
    },
    // 重置
    handleReset() {
      this.$refs.filterFormRef.resetFields('filterForm')
      this.handleFilter()
    },
    // 点击添加菜单按钮
    onAdd() {
      this.$refs.menuModal.action = 'add'
      this.$refs.menuModal.add()
    },
    onAdded() {
      this.getList()
    },
    // 点击删除按钮
    statusFlagChange(row) {
      const params = {
        menuId: row.menuId,
        statusFlag: row.statusFlag
      }
      // updateStatusSysMenu(params).then((resp) => {
      //   const type = resp.success ? 'success' : 'error'
      //   // 提示消息
      //   this.$dtMessage({
      //     type,
      //     message: '更新成功'
      //   })
      //   if (resp.success) {
      //     this.getList()
      //   }
      // })
    },
    // 点击删除按钮
    onDelete(row) {
      getVideoNum(row.id).then((res) => {
        let message
        if (res.data > 0) {
          message = `“${row.menuName}”目录下存在视频，确定要删除吗?`
        } else {
          message = `确定要删除“${row.menuName}”吗?`
        }
        this.$dtMessageBox({ title: '提示', message, type: 'warning' }).then(() => {
          del(row.id).then((resp) => {
            const type = resp.success ? 'success' : 'error'
            // 提示消息
            this.$dtMessage({
              type,
              message: '删除成功'
            })
            if (resp.success) {
              this.getList()
            }
          })
        })
      })
    },
    onExpandAll() {
      // this.tableData.forEach(value => {
      //   if (value.children && value.children.length > 0) {
      //     this.$refs.table.toggleRowExpansion(value, true)
      //   }
      // })
      this.refreshTable = false
      this.isExpandAll = true
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
    onFoldAll() {
      // this.tableData.forEach(value => {
      //   this.$refs.table.toggleRowExpansion(value, false)
      // })
      this.refreshTable = false
      this.isExpandAll = false
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
    onEdit(row) {
      this.$refs.menuModal.action = 'edit'
      this.$refs.menuModal.edit(row)
    },
    onEdited() {
      this.getList()
    }
  }
}
</script>

<style lang="scss">
  .menu{
    margin: 20px;
  }
  .tableScopeSwitch .el-switch__label {
    position: absolute;
    display: none;
    padding-left: 4px;
    color: #fff;
  }
  /*打开时文字位置设置*/
  .tableScopeSwitch .el-switch__label--right {
    z-index: 1;
    right: 20px;
  }
  /*关闭时文字位置设置*/
  .tableScopeSwitch .el-switch__label--left {
    z-index: 1;
    left: 18px;
  }
  /*显示文字*/
  .tableScopeSwitch .el-switch__label.is-active {
    display: block;
  }
</style>

// <style lang="scss" scoped>
//   @import '@/framework/styles/variables';
//   .el-dropdown-link {
//     cursor: pointer;
//     color: #F54A43;
//   }
//   .el-icon-arrow-down {
//     font-size: 12px;
//   }
// </style>
